import { useState, useEffect } from 'react';
import chatAPI from './ChatAPI';

export default function useFriendStatus(friendID) {
  const [isOnline, setIsOnline] = useState(null);

  useEffect(() => {
    function handleStatusChange(status) {
      setIsOnline(status.isOnline);
    }
    console.log('useFriendStatus start');

    chatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
    return () => {
      console.log('useFriendStatus end');
      chatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
    };
  });

  return isOnline;
}